﻿using System.Collections.Generic;
using System.Web;
using Lbb.Web;
using System;

namespace Comsenz.Base
{
    /// <summary>
    /// 通讯器基类
    /// </summary>
    /// <author>Chiron</author>
    public abstract class CommunicatorBase
    {
        /// <summary>
        /// 取得通信密钥
        /// </summary>
        /// <returns></returns>
        protected abstract string GetConnectKey();
        /// <summary>
        /// 构建参数
        /// </summary>
        /// <param name="args">参数</param>
        /// <returns></returns>
        protected virtual string MakeArgs(string args)
        {
            args = args ?? string.Empty;
            var agent = "&agent=" + Security.GetUserAgentMd5Code();
            return HttpUtility.UrlEncode(Security.Authcode(args + agent + "&time=" + PhpFunc.Time(), GetConnectKey(), Security.Operations.ENCODE));
        }
        /// <summary>
        /// 转为参数字符串
        /// </summary>
        /// <param name="args">参数</param>
        /// <returns></returns>
        protected string ToArgStr(IEnumerable<QueryParameter> args)
        {
            if (args == null) return string.Empty;
            string argStr = "", sep = "", qpv;
            if (args != null)
            {
                foreach (var qp in args)
                {
                    qpv = qp.ToUrlEncodedString();
                    if (!string.IsNullOrEmpty(qpv))
                    {
                        argStr += sep + qpv;
                        sep = "&";
                    }
                }
            }

            return argStr;
        }
        /// <summary>
        /// 构造发送给用户中心的请求数据
        /// </summary>
        /// <param name="module">请求的模块</param>
        /// <param name="action">请求的动作</param>
        /// <param name="argStr">参数（已加密）</param>
        /// <param name="extra">附加参数（传送时不加密）</param>
        /// <returns>用户中心处理后的反馈</returns>
        protected abstract string MakeRequestdata(string module, string action, string argStr, string extra);

        /// <summary>
        /// 与API交互
        /// </summary>
        /// <param name="postdata">请求数据</param>
        /// <returns>反馈数据</returns>
        protected abstract string Invoker(string postdata);

        /// <summary>
        /// 执行操作
        /// </summary>
        /// <param name="module">请求的模块</param>
        /// <param name="action">请求的动作</param>
        /// <param name="args">参数（会加密的方式传送）</param>
        /// <param name="extArgs">附加参数（传送时不加密）</param>
        /// <returns>反馈数据</returns>
        public virtual string Do(string module, string action, IEnumerable<QueryParameter> args = null, IEnumerable<QueryParameter> extArgs = null)
        {
            if (string.IsNullOrEmpty(module)) throw new ArgumentNullException("module");
            if (string.IsNullOrEmpty(action)) throw new ArgumentNullException("action");

            return ExceptionHandler.Handle(Invoker(MakeRequestdata(module, action, MakeArgs(ToArgStr(args)), ToArgStr(extArgs))));
        }
    }
}
